#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os,json,csv


str_instance='instance=~"10.148.128.([1-9]|1[0-2]).*|10.149.146.([5-9][0-9]|1[0-3][0-9]).*",'
str_time=os.popen('''date -d "90 seconds ago" +%s''').read()

# 获取磁盘剩余空间
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_filesystem_free_bytes{%smountpoint="/",fstype=~"ext.|xfs"}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_diskuse=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_filesystem_size_bytes{%smountpoint="/",fstype=~"ext.|xfs"}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_disktotal=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_memory_MemFree_bytes{%s}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_memfree=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_memory_Cached_bytes{%s}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_memcache=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_memory_Buffers_bytes{%s}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_membuffer=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_memory_MemTotal_bytes{%s}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_memtotal=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_load5{%s}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_load5=json.loads(data)
data=os.popen('''curl  -g 'localhost:9090/prometheus/api/v1/query?query=node_cpu_seconds_total{%smode="system"}&time=%s' ''' %(str_instance,str_time.strip())).read()
j_cpucount=json.loads(data)


last_dict={}


def get_instance(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]={'instance':in_json['data']['result'][i]['metric']['instance']}

def get_diskuse(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['diskuse']=in_json['data']['result'][i]['value'][1]

def get_disktotal(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['disktotal']=in_json['data']['result'][i]['value'][1]

def get_memfree(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['memfree']=in_json['data']['result'][i]['value'][1]

def get_memcache(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['memcache']=in_json['data']['result'][i]['value'][1]

def get_membuffer(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['membuffer']=in_json['data']['result'][i]['value'][1]

def get_memtotal(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['memtotal']=in_json['data']['result'][i]['value'][1]

def get_load5(in_json):
    for i in range(len(in_json['data']['result'])):
        last_dict[in_json['data']['result'][i]['metric']['instance']]['load5']=in_json['data']['result'][i]['value'][1]

def get_cpucount(in_json):
    for i in range(len(in_json['data']['result'])):

        if in_json['data']['result'][i]['metric']['instance'] in last_dict.keys():
            if last_dict[in_json['data']['result'][i]['metric']['instance']].get('cpucount'):
                last_dict[in_json['data']['result'][i]['metric']['instance']]['cpucount']+=1
            else:
                last_dict[in_json['data']['result'][i]['metric']['instance']]['cpucount']=1


get_instance(j_diskuse)
get_diskuse(j_diskuse)
get_disktotal(j_disktotal)
get_memfree(j_memfree)
get_memcache(j_memcache)
get_membuffer(j_membuffer)
get_memtotal(j_memtotal)
get_load5(j_load5)
get_cpucount(j_cpucount)

l_row=[]
l_values=[['IP','磁盘','内存','平均单核负载']]
    
for i in last_dict.values():
    l_row.extend([ \
        i['instance'],
        round(1-float(i['diskuse'])/float(i['disktotal']),2),
        round(1-(float(i['memfree'])+float(i['memcache'])+float(i['membuffer']))/float(i['memtotal']),2),
        round(float(i['load5'])/float(i['cpucount']),2)
    ])
    l_values.append(l_row)
    l_row=[]
with open('out.csv','a',encoding='utf-8') as csvfile:
    a=csv.writer(csvfile)
    a.writerows(l_values)

